//
//  LZBaseNavVC.swift
//  halikaPeso
//
//  Created by L-zh on 2024/12/16.
//

import Foundation
import UIKit

class LZBaseNavVC: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let dict:NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: APPScale(f: 32))]
        navigationBar.titleTextAttributes = dict as? [NSAttributedString.Key : Any]
        self.delegate = self as UINavigationControllerDelegate
        // Do any additional setup after loading the view.
        self.navigationBar.shadowImage = UIImage.init()
    
        let navBarBackgroundView = UIView(frame: CGRect(x: 0, y: 0, width: APPWidth, height: navHeight()))
        navBarBackgroundView.gradientColor(CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 0), [APPHexColor(hexValue: 0x2d82ff),APPHexColor(hexValue: 0x16b7ff)])
        UIGraphicsBeginImageContextWithOptions(CGSize(width: APPWidth, height: navHeight()), false, UIScreen.main.scale)
        navBarBackgroundView.layer.render(in: UIGraphicsGetCurrentContext()!)
        
        let navBarBackgroundImage = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();
        navigationBar.setBackgroundImage(navBarBackgroundImage, for: UIBarMetrics.default)
        
        self.navigationBar.tintColor = UIColor.white
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if (self.responds(to:#selector(getter: UINavigationController.interactivePopGestureRecognizer) )) {
            self.interactivePopGestureRecognizer?.isEnabled = false;
        }
        if children.count > 0 {
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "backWhite"), style: .plain, target: self, action: #selector(backBarButtonClick))
        }
        super .pushViewController(viewController, animated: animated)
    }
 
}

extension LZBaseNavVC : UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if (self.responds(to:#selector(getter: UINavigationController.interactivePopGestureRecognizer) )) {
            self.interactivePopGestureRecognizer?.isEnabled = true;
        }
        if viewControllers.count == 1 {
            self.interactivePopGestureRecognizer?.isEnabled = false;
            self.interactivePopGestureRecognizer?.delegate = nil
        }
    }
    
    @objc func backBarButtonClick() {
        popViewController(animated: true)
    }
}




